/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.hibernate.spring;

import com.knezevic.realestate.Advert;
import com.knezevic.realestate.beans.AdvertBean;
import com.knezevic.realestate.service.Adverts;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Nemanja
 */
public class AdvertsDAO implements Adverts{
    private SessionFactory sessionFactory;

     public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Transactional
    public Advert save(Advert advert) {
        Long id = (Long) sessionFactory.getCurrentSession().save(advert);
        return (Advert) sessionFactory.getCurrentSession().get(AdvertBean.class, id);
    }

    @Transactional
    public void update(Advert advert) {
        sessionFactory.getCurrentSession().merge(advert);
    }

    @Transactional
    public void deleteAdvert(Advert advert) {
        sessionFactory.getCurrentSession().delete(advert);
    }

    @Transactional
    public Collection<Advert> retrieve() {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(AdvertBean.class);
        return criteria.list();
    }

    @Transactional
    public Advert retrieve(Serializable id) {
        return (Advert) sessionFactory.getCurrentSession().get(AdvertBean.class, id);
    }
}
